package com.itheima.reggie_test.web;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie_test.common.Result;
import com.itheima.reggie_test.entity.Category;
import com.itheima.reggie_test.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ auther: 小熊
 * @ date:2022/12/15 16:38
 * @Todo :
 */
@RestController
@RequestMapping("/category")
public class CategoryContorller {

    @Autowired
    private ICategoryService categoryService;

    /**
     * 新增
     * @param category
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Category category){
        categoryService.save(category);
        return Result.success("添加成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<Page> quetyToPage(int page,int pageSize){
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo,wrapper);
        return Result.success(pageInfo);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping
    public Result removeById(Long id){
        Result result = categoryService.removeFromId(id);
        return result;
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    public Result udpate(@RequestBody Category category){
        categoryService.updateById(category);
        return Result.success("修改成功");
    }

    @GetMapping("/list")
    public Result getCategory(Category category){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(category.getType() != null,Category::getType,category.getType());
        List<Category> list = categoryService.list(wrapper);
        return Result.success(list);
    }

}
